package ControlLayer;

import java.util.ArrayList;
import DBLayer.DBConnection;
import DBLayer.DBPerson;
import DBLayer.IFDBPerson;
import ModelLayer.Customer;
import ModelLayer.Employee;
import ModelLayer.Person;

public class PersonCtr {
	
	public PersonCtr()
	{
		
	}
	
	//This method return all the customer in an Arraylist.
	public ArrayList<Customer> listAllCustomers()
	{
		IFDBPerson dbPer = new DBPerson();
		ArrayList<Customer> allPer;
		allPer = dbPer.listAllCustomers(false); 
		return allPer;
	}
	
	//This method returns all the persons (EMP AND CUS) in the DB
	public ArrayList<Person> listAllPersons()
	{
		ArrayList<Person> persons = new ArrayList<Person>();
		persons.addAll(listAllEmployees());
		persons.addAll(listAllCustomers());
		return persons;
	}
	//This method return all the employee in an Arraylist.
	public ArrayList<Employee> listAllEmployees()
	{
		IFDBPerson dbPer = new DBPerson();
		ArrayList<Employee> allPer = new ArrayList<Employee>();
		allPer = dbPer.listAllEmployees(false);
		
		return allPer;
	}
	
	
	//This method search for a customer through DB on their phoneNumer(Customer ID).
	public Customer findCustomer(int phoneNumber)
	{
		IFDBPerson dbPer = new DBPerson();
		return dbPer.findCustomer(phoneNumber, false);
	}
	
	//This method search for an employee through DB on their phoneNumer(Employee ID).
	public Employee findEmployee(int phoneNumber)
	{
		IFDBPerson dbPer = new DBPerson();
		return dbPer.findEmployee(phoneNumber, false);
	}
	
	
	
	//This method creates a new Customer in DBPerson with name, adress, zipCode, phoneNumber, email, customerType, companyName and cvrNr.
	public void insertCustomer(String name, String adress, int zipCode, int phoneNumber, String email, String customerType, String companyName, int cvrNr)
	{
		
		Customer cusObj = new Customer();
		
		cusObj.setName(name);
		cusObj.setAdress(adress);
		cusObj.setZipCode(zipCode);
		cusObj.setPhoneNumber(phoneNumber);
		cusObj.setEmail(email);
		cusObj.setCustomerType(customerType);
		cusObj.setCompanyName(companyName);
		cusObj.setCvrNr(cvrNr);
		
		try{
			DBConnection.startTransaction();
			IFDBPerson dbPer = new DBPerson();
			dbPer.insertCustomer(cusObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//This methods creates a new Employee in DBPerson with name, adress, zipCode, phoneNumber, email and jobType.
	public void insertEmployee(String name, String adress, int zipCode, int phoneNumber, String email, String jobType)
	{
		
		Employee empObj = new Employee();
		
		empObj.setName(name);
		empObj.setAdress(adress);
		empObj.setZipCode(zipCode);
		empObj.setPhoneNumber(phoneNumber);
		empObj.setEmail(email);
		empObj.setJobType(jobType);
		
		try
		{
			DBConnection.startTransaction();
			IFDBPerson dbPer = new DBPerson();
			dbPer.insertEmployee(empObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//This method update the customer with the new values of name, adress, zipCode, phoneNumber, email, customerType, companyName and cvrNr.
	public int updateCustomer(String name, String adress, int zipCode, int phoneNumber, String email, String customerType, String companyName, int cvrNr)
	{
		IFDBPerson dbPer = new DBPerson();
		Customer cusObj = new Customer();
		
		cusObj.setName(name);
		cusObj.setAdress(adress);
		cusObj.setZipCode(zipCode);
		cusObj.setPhoneNumber(phoneNumber);
		cusObj.setEmail(email);
		cusObj.setCustomerType(customerType);
		cusObj.setCompanyName(companyName);
		cusObj.setCvrNr(cvrNr);
		return dbPer.updateCustomer(cusObj);
	}
	
	//This method update the employee with the new values of name, adress, zipCode, city, phoneNumber, email and jobType.
	public int updateEmployee(String name, String adress, int zipCode, int phoneNumber, String email, String jobType)
	{
		IFDBPerson dbPer = new DBPerson();
		Employee empObj = new Employee();
		
		empObj.setName(name);
		empObj.setAdress(adress);
		empObj.setZipCode(zipCode);
		empObj.setPhoneNumber(phoneNumber);
		empObj.setEmail(email);
		empObj.setJobType(jobType);
		return dbPer.updateEmployee(empObj);
	}
	
	//This method delete a customer in DBPerson on the phoneNumber.
	public void deletePerson(int phoneNumber)
	{
		DBPerson dbPerson = new DBPerson();
		dbPerson.deletePerson(phoneNumber);
	}

	
	//Returns the city name after a zipcode is entered
	public String getCityOnZipCode(int zipCode)
	{
		DBPerson dbPerson = new DBPerson();
		String city = dbPerson.getCityOnZipCode(zipCode);
		return city;
	}
}
